package com.yh.workbill.ui.companyInfo.mvp;

import android.content.Intent;
import android.content.SharedPreferences;

import com.yh.workbill.App;
import com.yh.workbill.AppGlobals;
import com.yh.workbill.base.IBaseModel;
import com.yh.workbill.db.UserInfo;
import com.yh.workbill.db.UserInfoDao;
import com.yh.workbill.model.Company;
import com.yh.workbill.network.BaseCallback;
import com.yh.workbill.network.OkHttpService;

import java.net.InterfaceAddress;
import java.security.Signature;
import java.util.List;

import androidx.arch.core.executor.ArchTaskExecutor;

import static com.yh.workbill.utils.Config.SP_USER;

public class AddCompanyInfo_Model {

    public interface CompanyDataCallBack extends IBaseModel.Fail {
        void onSuccess(List<Company> data);
    }

    public interface AddCallBack extends IBaseModel.Fail {
        void onSuccess(String company, String company_address, String company_phone);
    }

    public interface DelCallBacl extends IBaseModel.Fail {
        void onSuceess(String company);
    }

    public void getData(int pageindex, int pagenumber, CompanyDataCallBack callBack) {
        ArchTaskExecutor.getIOThreadExecutor().execute(() -> {
            SharedPreferences sp = AppGlobals.getApplication().getSharedPreferences(SP_USER, AppGlobals.getApplication().MODE_PRIVATE);
            String phone = sp.getString("phone", "");
            UserInfoDao userInfoDao = App.appDataBase.getUserInfoDao();
            UserInfo singleUserInfo = userInfoDao.getSingleUserInfo(phone);
            OkHttpService.getInstance().getAllCompany(singleUserInfo.getUuid(), pageindex, pagenumber, new BaseCallback<List<Company>>() {
                @Override
                public void accessSuccess(int code, String msg, List<Company> data) {
                    ArchTaskExecutor.getMainThreadExecutor().execute(() -> {
                        if (code == 200) {
                            callBack.onSuccess(data);
                        } else {
                            callBack.fail(msg);
                        }
                    });
                }

                @Override
                public void accessFailure(String errorMessage) {
                    ArchTaskExecutor.getMainThreadExecutor().execute(() -> {
                        callBack.fail(errorMessage);
                    });
                }
            });
        });
    }

    public void addCompanyInfo(String company_name, String company_address, String company_phone, AddCallBack callBack) {
        ArchTaskExecutor.getIOThreadExecutor().execute(() -> {
            SharedPreferences sp = AppGlobals.getApplication().getSharedPreferences(SP_USER, AppGlobals.getApplication().MODE_PRIVATE);
            String phone = sp.getString("phone", "");
            UserInfoDao userInfoDao = App.appDataBase.getUserInfoDao();
            UserInfo singleUserInfo = userInfoDao.getSingleUserInfo(phone);
            OkHttpService.getInstance().postAddCompanyName(singleUserInfo.getUuid(), company_name, company_address, company_phone, new BaseCallback<String>() {
                @Override
                public void accessSuccess(int code, String msg, String data) {
                    ArchTaskExecutor.getMainThreadExecutor().execute(() -> {
                        if (code == 200) {
                            callBack.onSuccess(company_name, company_address, company_phone);
                        } else {
                            callBack.fail(msg);
                        }
                    });
                }

                @Override
                public void accessFailure(String errorMessage) {
                    ArchTaskExecutor.getMainThreadExecutor().execute(() -> {
                        callBack.fail(errorMessage);
                    });
                }
            });
        });
    }

    public void delCompanyInfo(String company, String uuid, DelCallBacl callBack) {
        ArchTaskExecutor.getIOThreadExecutor().execute(() -> {
            SharedPreferences sp = AppGlobals.getApplication().getSharedPreferences(SP_USER, AppGlobals.getApplication().MODE_PRIVATE);
            String phone = sp.getString("phone", "");
            UserInfoDao userInfoDao = App.appDataBase.getUserInfoDao();
            UserInfo singleUserInfo = userInfoDao.getSingleUserInfo(phone);
            OkHttpService.getInstance().delCompany(singleUserInfo.getUuid(), uuid, new BaseCallback<String>() {
                @Override
                public void accessSuccess(int code, String msg, String data) {
                    ArchTaskExecutor.getMainThreadExecutor().execute(() -> {
                        if (code == 200) {
                            callBack.onSuceess(company);
                        } else {
                            callBack.fail(msg);
                        }
                    });
                }

                @Override
                public void accessFailure(String errorMessage) {
                    ArchTaskExecutor.getMainThreadExecutor().execute(() -> callBack.fail(errorMessage));
                }
            });

        });
    }

}
